home *** CD-ROM | disk | FTP | other *** search
- /*
- File: Sample.c
-
- Contains: Location Manager SDK Sample main code...
-
- Version: ALM SDK 1.0
- Package: Location Manager SDK 1.0
-
- Copyright: © 1984-1997 by Apple Computer, Inc.
- All rights reserved.
-
- Bugs?: If you find a problem with this file, use the Apple Bug Reporter
- stack. Include the file and version information (from above)
- in the problem description and send to:
- Internet: apple.bugs@apple.com
-
- */
-
- #include <ConditionalMacros.h>
-
- #if !GENERATING68K | GENERATINGCFM
- #error This project MUST be classic 68K!
- #endif
-
- // -------------------------------------------------------------------------------------
-
- // Project Includes...
-
- #ifndef __SAMPLE__
- #include "Sample.h"
- #endif // __SAMPLE__
-
- #ifndef __UTILITIES__
- #include "Utilities.h"
- #endif // __UTILITIES__
-
- // SDK Includes...
-
- #define ALM_BASENAME()
- #define ALM_GLOBALS() GlobalsHandle
-
- #ifndef __LOCATIONMANAGER_K__
- #include <LocationManager.k.h>
- #endif // __LOCATIONMANAGER_K__
-
- // MacOS Includes...
-
- #ifndef __COMPONENTS__
- #include <Components.h>
- #endif // __COMPONENTS__
-
- #ifndef __FONTS__
- #include <Fonts.h>
- #endif // __FONTS__
-
- #ifndef __PLSTRINGFUNCS__
- #include <PLStringFuncs.h>
- #endif // __PLSTRINGFUNCS__
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif // __RESOURCES__
-
- #ifndef __TEXTUTILS__
- #include <TextUtils.h>
- #endif // __TEXTUTILS__
-
- // -------------------------------------------------------------------------------------
-
- // Readability Constants...
-
- #define kInvalidFunction ((ComponentFunctionUPP) -1)
-
- #define kDefaultINTLresources NULL
- #define kNoSeconds false
-
- // -------------------------------------------------------------------------------------
-
- // Local prototypes...
-
- static Boolean
- CallSupported (SInt16 selector);
- // Return true if the specified selector code is one this module supports...
- static ComponentResult
- HandleComponentManagerCall (ComponentParameters* params, SInt16 selector);
- // Dispatch a component manager call (selector < 0)...
- static ComponentResult
- HandleLocationManagerCall (ComponentParameters* params, SInt16 selector,
- Handle globals);
- // Dispatch a Location Manager specific call...
-
- // Component prototypes...
-
- static pascal ComponentResult
- Open (ComponentInstance self);
- // Initialize the modules's global variables, etc...
- static pascal ComponentResult
- Close (ComponentInstance self);
- // Deinitialize the modules's global variables...
-
- // --------------------------------------------------------------------------------------
-
- // Main entry point (interface as expected by component manager)...
-
- pascal ComponentResult
- main (ComponentParameters* params, Handle storage) {
-
- // We dispatch the calls from here; also, for ALM calls, we'll make sure the
- // current resource fork is ours...
-
- ComponentResult result;
- SInt16 selector = params->what;
- SInt16 svResFile = CurResFile ();
-
- if (selector < 0) {
- // Component manager request codes are negative...
- result = HandleComponentManagerCall (params, selector);
- } else {
- // ALM selectors...
- if (storage != NULL) {
- UseResFile ((**(GlobalsHandle) storage).resFile);
- } // if
- result = HandleLocationManagerCall (params, selector, storage);
- } // if
-
- UseResFile (svResFile);
-
- return result;
-
- } // main
-
- // -------------------------------------------------------------------------------------
-
- static Boolean
- CallSupported (SInt16 selector) {
-
- Boolean response;
-
- switch (selector) {
- case kALMGetCurrentSelect:
- case kALMSetCurrentSelect:
- // case kALMEditSettingSelect:
- case kALMCompareSettingSelect:
- case kALMDescribeSettingsSelect:
- case kALMDescribeErrorSelect:
- case kALMImportExportSelect:
- response = true;
- break;
- default:
- response = false;
- break;
- } // switch
-
- return response;
-
- } // CallSupported
-
- // -------------------------------------------------------------------------------------
-
- static ComponentResult
- HandleComponentManagerCall (ComponentParameters* params, SInt16 selector) {
-
- ComponentResult result = (ComponentResult) noErr;
- ComponentFunctionUPP func = NULL;
-
- switch (selector) {
-
- case kComponentOpenSelect:
- func = (ComponentFunctionUPP) Open;
- break;
- case kComponentCloseSelect:
- func = (ComponentFunctionUPP) Close;
- break;
- case kComponentCanDoSelect:
- result = CallSupported (*(SInt16*) params->params);
- break;
- case kComponentVersionSelect:
- result = kModuleVersion;
- break;
- case kComponentRegisterSelect:
- result = false; // false means "yes, please register me"
- break;
- case kComponentTargetSelect :
- case kComponentUnregisterSelect :
- result = badComponentSelector;
- break;
-
- } // switch
-
- if (func != NULL) {
- result = CallComponentFunction (params, func);
- } // if
-
- return result;
-
- } // HandleComponentManagerCall
-
- // -------------------------------------------------------------------------------------
-
- static ComponentResult
- HandleLocationManagerCall (ComponentParameters* params, SInt16 selector,
- Handle globals) {
-
- ComponentResult result = (ComponentResult) noErr;
- ComponentFunctionUPP func = NULL;
-
- switch (selector) {
-
- case kALMGetCurrentSelect:
- func = (ComponentFunctionUPP) GetCurrent;
- break;
- case kALMSetCurrentSelect:
- func = (ComponentFunctionUPP) SetCurrent;
- break;
- case kALMCompareSettingSelect:
- func = (ComponentFunctionUPP) CompareSetting;
- break;
- //case kALMEditSettingSelect:
- // func = (ComponentFunctionUPP) EditSetting;
- // break;
- case kALMDescribeSettingsSelect:
- func = (ComponentFunctionUPP) DescribeSettings;
- break;
- case kALMDescribeErrorSelect:
- func = (ComponentFunctionUPP) DescribeError;
- break;
- case kALMImportExportSelect:
- func = (ComponentFunctionUPP) ImportExport;
- break;
- case kALMGetScriptInfoSelect:
- func = (ComponentFunctionUPP) GetScriptInfo;
- break;
- case kALMGetInfoSelect:
- func = (ComponentFunctionUPP) GetInfo;
- break;
- default:
- func = kInvalidFunction;
- break;
-
- } // switch
-
- if (func == kInvalidFunction) {
- result = badComponentSelector;
- } else if (func != NULL) {
- result = CallComponentFunctionWithStorage (globals, params, func);
- } // if
-
- return result;
-
- } // HandleLocationManagerCall
-
- // -------------------------------------------------------------------------------------
-
- static pascal ComponentResult
- Open (ComponentInstance self) {
-
- // Keep the Open routine lightweight, and only fail in the case of dire
- // emergencies that indicate a major problem...
-
- OSErr err = noErr;
- GlobalsHandle globals = (GlobalsHandle) NewHandle (sizeof (Globals));
-
- if (globals != NULL) {
-
- SetComponentInstanceStorage (self, (Handle) globals);
-
- (**globals).fileCount = 0;
- (**globals).self = (Component) self;
- (**globals).resFile = OpenComponentResFile ((Component) self);
-
- err = ResError ();
-
- } else {
- err = memFullErr;
- } // if
-
- return (ComponentResult) err;
-
- } // Open
-
- // -------------------------------------------------------------------------------------
-
- static pascal ComponentResult
- Close (ComponentInstance self) {
-
- OSErr err = noErr;
- GlobalsHandle globals = (GlobalsHandle) GetComponentInstanceStorage (self);
-
- if (globals != NULL) {
-
- SInt16 resFile = (**globals).resFile;
-
- if (resFile > 0) {
- CloseComponentResFile (resFile);
- (**globals).resFile = 0;
- } // if
-
- DisposeHandle ((Handle) globals);
- SetComponentInstanceStorage (self, NULL);
-
- } // if
-
- return (ComponentResult) err;
-
- } // Close
-
- // -------------------------------------------------------------------------------------
-
- static pascal ComponentResult
- GetCurrent (GlobalsHandle globals, Handle setting) {
-
- OSErr err = noErr;
-
- // To get the current settings, we need to go off to the resource and make FSSpecs
- // for all of the files we care about, then read in their settings...
-
- err = ConfirmFSSpecs (globals);
-
- if (err == noErr) {
- err = ReadSetting (globals, (SettingHandle) setting);
- } // if
-
- return (ComponentResult) err;
-
- } // GetCurrent
-
- // -------------------------------------------------------------------------------------
-
- static pascal ComponentResult
- SetCurrent (GlobalsHandle globals, Handle setting, ALMRebootFlags* flags) {
-
- OSErr err = noErr;
- Handle tempSetting = NewHandle (1);
-
- *flags = kALMNoChange; // Safe value...
-
- // Before we go off and change the setting, see if it's the current setting; if
- // the settings are _not_ the same, we'll assume "kALMAvailableNow" and let
- // UseSetting escalate it, based on any of the specific files...
-
- if (tempSetting != NULL) {
- err = (OSErr) GetCurrent (globals, tempSetting);
- if (err == noErr) {
- Boolean lookTheSame;
- err = CompareSetting (globals, tempSetting, setting, &lookTheSame);
- if ((err == noErr) && !lookTheSame) {
- *flags = kALMAvailableNow;
- err = UseSetting (globals, (SettingHandle) setting, flags);
- } // if
- } // if
- } else {
- err = memFullErr;
- } // if
-
- if (tempSetting != NULL) {
- DisposeHandle (tempSetting);
- } // if
-
- return (ComponentResult) err;
-
- } // SetCurrent
-
- // -------------------------------------------------------------------------------------
-
- static pascal ComponentResult
- CompareSetting (GlobalsHandle globals, Handle setting1, Handle setting2,
- Boolean* equal) {
-
- #ifndef __SC__
- #pragma unused (globals)
- #endif // __SC__
-
- OSErr err = noErr;
- SInt8 svState1 = HGetState (setting1);
- SInt8 svState2 = HGetState (setting2);
- SettingPtr set1;
- SettingPtr set2;
-
- *equal = true;
-
- // To see if two settings are the same, all we do is compare their dates...
-
- HLock (setting1);
- HLock (setting2);
-
- set1 = (SettingPtr) *setting1;
- set2 = (SettingPtr) *setting2;
-
- do {
-
- UInt32 fileIndex;
- UInt32 fileCount = set1->fileCount;
-
- // If the settings versions are not the same, we have a problem...
-
- if (set1->version != set2->version) {
- *equal = false;
- break;
- } // if
-
- // If the number of files modified is not the same, we have a problem...
-
- if (fileCount != set2->fileCount) {
- *equal = false;
- break;
- } // if
-
- // Now, if the modification dates (or creation dates) changed, we'll take
- // that as a signal
-
- for (fileIndex = 0; fileIndex < fileCount; fileIndex += 1) {
- if ((set1->fileInfo[fileIndex].dateModified !=
- set2->fileInfo[fileIndex].dateModified) ||
- (set1->fileInfo[fileIndex].dateCreated !=
- set2->fileInfo[fileIndex].dateCreated)) {
- *equal = false;
- break;
- } // if
- } // for
-
- } while (false); // Loop once...
-
- HSetState (setting2, svState2);
- HSetState (setting1, svState1);
-
- return (ComponentResult) err;
-
- } // CompareSetting
-
- // -------------------------------------------------------------------------------------
-
- static pascal ComponentResult
- EditSetting (GlobalsHandle globals, Handle setting) {
-
- #ifndef __SC__
- #pragma unused (globals, setting)
- #endif // __SC__
-
- OSErr err = noErr;
-
- // Not implemented or called; not sure what the content of setting is...
-
- return (ComponentResult) err;
-
- } // EditSetting
-
- // -------------------------------------------------------------------------------------
-
- static pascal ComponentResult
- DescribeSettings (GlobalsHandle globals, Handle setting, CharsHandle text) {
-
- OSErr err = noErr;
- SettingHandle theSet = (SettingHandle) setting;
- UInt32 dateMod;
- Str255 dateModStr;
- Str255 timeModStr;
- Str63 firstFileStr;
- Str255 templateStr;
-
- // Not entirely sure how to describe the settings, but we'll take the approach
- // of using the first file name and its modification date as "sufficient" info...
-
- err = ConfirmFSSpecs (globals);
-
- if (err == noErr) {
-
- SInt8 svState;
- UInt8 strSize;
-
- dateMod = (**theSet).fileInfo[0].dateModified;
-
- DateString (dateMod, shortDate, dateModStr, kDefaultINTLresources);
- TimeString (dateMod, kNoSeconds, timeModStr, kDefaultINTLresources);
- GetIndString (templateStr, kSampleMiscStrRsrcID, kSampleDescribeTemplateIdx);
-
- svState = HGetState ((Handle) globals);
- HLock ((Handle) globals);
- PLstrcpy (firstFileStr, (**globals).theFiles[0].name);
- HSetState ((Handle) globals, svState);
-
- InsParamStr ("\p^0", firstFileStr, templateStr);
- InsParamStr ("\p^1", timeModStr, templateStr);
- InsParamStr ("\p^2", dateModStr, templateStr);
-
- strSize = StrLength (templateStr);
- SetHandleSize ((Handle) text, strSize);
- err = MemError ();
- if (err == noErr) {
- BlockMoveData (&templateStr[1], *text, strSize);
- } // if
-
- } // if
-
- return (ComponentResult) err;
-
- } // DescribeSettings
-
- // -------------------------------------------------------------------------------------
-
- static pascal ComponentResult
- DescribeError (GlobalsHandle globals, OSErr lastErr, Str255 errStr) {
-
- #ifndef __SC__
- #pragma unused (globals)
- #endif // __SC__
-
- OSErr err = noErr;
-
- // We're just going to be very superficial about this, and deal with a very few
- // "plain text" messages only. We could get fancy, and try to be a bit more
- // descriptive...
-
- if ((lastErr > 0) && (lastErr <= kSampleBiggestKnownIdx)) {
- GetIndString (errStr, kSampleErrStrRsrcID, lastErr);
- } else {
- err = paramErr; // Get ALM to generate default "error number" text...
- } // if
-
- return (ComponentResult) err;
-
- } // DescribeError
-
- // -------------------------------------------------------------------------------------
-
- static pascal ComponentResult
- ImportExport (GlobalsHandle globals, Boolean import, Handle setting, short resRefNum) {
-
- #ifndef __SC__
- #pragma unused (resRefNum)
- #endif // __SC__
-
- OSErr err = noErr;
-
- // Not a lot of work to do here; but if we are importing _and_ the current settings
- // on the machine match the import, we have a problem in that our mechanism doesn't
- // let us distinguish the new setting properly. It is possible that the user would
- // prefer to overwrite the existing settings, but we won't handle that case...
-
- if (import) {
-
- Handle tempSetting = NewHandle (1);
-
- if (tempSetting != NULL) {
- err = (OSErr) GetCurrent (globals, tempSetting);
- if (err == noErr) {
- Boolean lookTheSame;
- err = CompareSetting (globals, tempSetting, setting, &lookTheSame);
- if ((err == noErr) && lookTheSame) {
- err = kSampleImportPrefDatesIdx;
- } // if
- } // if
- } else {
- err = memFullErr;
- } // if
-
- if (tempSetting != NULL) {
- DisposeHandle (tempSetting);
- } // if
-
- } // if
-
- return (ComponentResult) err;
-
- } // ImportExport
-
- // -------------------------------------------------------------------------------------
-
- static pascal ComponentResult
- GetScriptInfo (GlobalsHandle globals, ALMScriptMgrInfo* info) {
-
- #ifndef __SC__
- #pragma unused (globals)
- #endif // __SC__
-
- OSErr err = noErr;
- INTLinfoHandle scriptInfo = NULL;
-
- // We stored this stuff in a resource, so it can be localized easily; we have
- // to do a bit of translation, though, from our resource format to the ALM format...
-
- scriptInfo = (INTLinfoHandle) GetResource (kINTLrsrcType, kINTLrsrcID);
-
- if (scriptInfo != NULL) {
-
- HLock ((Handle) scriptInfo);
-
- info->version = (**scriptInfo).version;
- info->scriptCode = (**scriptInfo).scriptCode;
- info->regionCode = (**scriptInfo).regionCode;
- info->langCode = (**scriptInfo).langCode;
- info->fontSize = (**scriptInfo).fontSize;
- GetFNum ((**scriptInfo).fontName, &info->fontNum);
-
- HUnlock ((Handle) scriptInfo);
-
- } else {
- err = ResError ();
- } // if
-
- if (scriptInfo != NULL) {
- ReleaseResource ((Handle) scriptInfo);
- } // if
-
- return (ComponentResult) err;
-
- } // GetScriptInfo
-
- // -------------------------------------------------------------------------------------
-
- static pascal ComponentResult
- GetInfo (GlobalsHandle globals, CharsHandle* text, STHandle* style,
- ModalFilterUPP filter) {
-
- #ifndef __SC__
- #pragma unused (globals, filter)
- #endif // __SC__
-
- OSErr err = noErr;
-
- // This code is pretty cookie-cutter, unless you want to make it context-
- // sensitive...
-
- do {
-
- *text = (CharsHandle) Get1Resource ('TEXT', kGetInfoRsrcID);
- err = ResError ();
- if (err != noErr) {
- break;
- } // if
- *style = (STHandle) Get1Resource ('styl', kGetInfoRsrcID);
- err = ResError ();
- if (err != noErr) {
- break;
- } // if
- DetachResource ((Handle) *text);
- DetachResource ((Handle) *style);
-
- } while (false); // execute once...
-
- return (ComponentResult) err;
-
- } // GetInfo
-